<?php
/* explain:		news system
 * Project:     
 * File:        news.php
 *
 * @ link 		http://www.../modules/
 * @ Email		ldmmyx@hotmail.com
 * @ copyright 	2005  Ling Deming
 * @ author 	ivan.ling  Ling Deming
 * @ version 	1.0
 *
 */
include_once("Common.php");
include_once("Logs.php");
class Merchant extends Common
{
	public $intLinkSign;
	public $errorInfo;
	
	private $iUserID;
	
	private $objLog;

	function Merchant($strLanguage, $userid)
	{
		parent::Common();
		
		$this->intLinkSign = $this->getConnection($strLanguage);
		
		$this->objLog 	= new Logs($userid);
		$this->iUserID 	= $userid;
	}

	/* get data via ID
	 *
	 * @param  int    id
	 * @return mixed  false:
	 */
	public function getByID($arrPopedom = array(), $id)
	{
		if($this->isUnallowableView($arrPopedom))
			die($this->getNotPopedomInfo());
		
		$sql = "SELECT * FROM merchant WHERE id = $id";
		$rs  = mysql_query($sql, $this->intLinkSign);
		if($rs)
			return mysql_fetch_array($rs);
		else
			return false;
	}

	/* 
	 *
	 * @param  string 	userid		
	 * @param  string 	number		man number  
	 * @param  string  	position    position
	 * @param  string 	summary		summary 
	 * @param  string 	descript	descript  
	 * @param  datetime	startDate	start date
	 * @param  datetime endDate		end date
 	 * @return int    	true: insert ID
	 */
    public function add($arrPopedom = array(), $type, $name, $descript, $authorized, $CSPhone, $CSEmail, $url, $rating, $displayURL, $sitemapPriority, $status, $headline)
	{
		if($this->isUnallowableAdd($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_ADD);
			
		$sql = "INSERT INTO merchant SET name='$name', descript='$descript', type = '$type', authorized = '$authorized', CSPhone = '$CSPhone', CSEmail = '$CSEmail', " .
				"url = '$url', rating = '$rating', displayURL = '$displayURL', sitemapPriority = '$sitemapPriority', status = '$status', headline = '$headline', createdTime = UTC_TIMESTAMP()";
					
		$rs  = mysql_query($sql, $this->intLinkSign) or die (mysql_error($this->intLinkSign));
		if($rs)
			return true;
		else
			return false;
	}

	/* edit
	 *
	 * @param  string 	userid		news class name 
	 * @param  string 	number		man number  
	 * @param  string  	position    position
	 * @param  string 	summary		summary 
	 * @param  string 	descript	descript  
	 * @param  datetime	startDate	start date
	 * @param  datetime endDate		end date
 	 	 * @param  return 	boolean
	 */
	public function edit($arrPopedom = array(), $id, $type, $name, $descript, $authorized, $CSPhone, $CSEmail, $url, $rating, $displayURL, $sitemapPriority, $status, $headline)
	{
		if($this->isUnallowableEdit($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_VIEW);
			
		$sql = "UPDATE merchant SET name='$name', descript='$descript', type = '$type', authorized = '$authorized', CSPhone = '$CSPhone', CSEmail = '$CSEmail', " .
				"url = '$url', rating = '$rating', displayURL = '$displayURL', sitemapPriority = '$sitemapPriority', status = '$status', headline = '$headline', createdTime = UTC_TIMESTAMP() WHERE id = $id";
		$rs	 = mysql_query($sql, $this->intLinkSign) or die (mysql_error($this->intLinkSign));
		if($rs)
			return true;
		else
			return false;
	}
	
	/* delete news class
	 *
	 * @param  int    	id
	 * @param  string  	status
	 * @param  return 	boolean
	 */
	public function delete($arrPopedom = array(), $id)
	{
		if($this->isUnallowableDelete($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_DELETE, "id=$id");
			
		$sql = "DELETE FROM merchant WHERE id = $id";
		
		$rs	 = mysql_query($sql, $this->intLinkSign) or die (mysql_error());
		if($rs)
			return true;
		else
			return false;
	}
	
		/* 
	 *
	 * @param  string  	name 	
	 * @param  string 	namecn
	 * @param  string 	status 		normal, stop
	 * @return boolean 	true or fail
	 * 
	 */
	public function addImage($arrPopedom = array(), $merchantID, $imageID, $isMain = 'false')
	{
		if($this->isUnallowableAdd($arrPopedom))
			die($this->getNotPopedomInfo());

		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_ADD);
			
		$sql = "INSERT INTO merchant_images SET merchantID = '$merchantID', imageID='$imageID', isMain = '$isMain'";

		$rs = mysql_query($sql, $this->intLinkSign) or die(mysql_error($this->intLinkSign));  
		
		return $rs ? true: false;
	}
	
	public function deleteImage($arrPopedom = array(), $merchantID, $imageID)
	{
		if($this->isUnallowableAdd($arrPopedom))
			die($this->getNotPopedomInfo());

		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_ADD);
			
		$sql = "DELETE FROM merchant_images WHERE merchantID = '$merchantID' AND imageID='$imageID'";

		$rs = mysql_query($sql, $this->intLinkSign) or die(mysql_error($this->intLinkSign));  
		
		return $rs ? true: false;
	}
	
	/* list
	 *
	 * @ param  string 	class id
	 * @ param  string 	page
	 * @ param  string 	discount
	 * @ param  string 	url
	 * @ param  return 	array
	 */
	public function lists($arrPopedom = array(), $where, $page = '', $discount = '', $url = '')
	{
		if($this->isUnallowableView($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_VIEW);
			
		$arrback = array();

		$sql = "SELECT count(*) FROM merchant$where";

		$rs  = mysql_query($sql, $this->intLinkSign) or die (mysql_error($this->intLinkSign));
		list($total) = mysql_fetch_row($rs);
		$this->mutilPager($total, $page, $discount, $url);
		if(empty($total))
 			return $arrback;

		if(empty($discount))
			$sql = "SELECT * FROM merchant$where";
		else{
			$sql = "SELECT * FROM merchant$where LIMIT $this->firstcount, $discount";
		}
		$rs  = mysql_query($sql, $this->intLinkSign);
		if($rs){
			while(is_array($arrtmp = mysql_fetch_array($rs))){
				$arrback[] = $arrtmp;
			}
			return $arrback;
		}else
 			return $arrback;
	}
	
	/* list by condition
	 *
	 * @param  string condition
	 * @return mixed  false: sql error  array: news info
	 */
	public function listByCustom($arrPopedom = array(), $condition='')
	{
		if($this->isUnallowableView($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_VIEW);
			
		$arrBack = array();
		$sql = "SELECT * FROM merchant".$condition;
        $rs  = mysql_query($sql, $this->intLinkSign);
		if($rs){
			while($arrCache = mysql_fetch_array($rs)){
				$arrBack[] = $arrCache;
			}
			return $arrBack;
		}else
			return false;
	}

		/* list by condition
	 *
	 * @param  string condition
	 * @return mixed  false: sql error  array: news info
	 */
	public function listSimple($arrPopedom = array(), $where = '')
	{
		if($this->isUnallowableView($arrPopedom))
			die($this->getNotPopedomInfo());
		
		$arrBack = array();
		$sql = "SELECT id, name FROM merchant $where";
        $rs  = mysql_query($sql, $this->intLinkSign);
		if($rs){
			while($arrCache = mysql_fetch_array($rs)){
				$arrBack[$arrCache['id']] = $arrCache['name'];
			}
			return $arrBack;
		}else
			return false;
	}
	
	public function IsExistName($name)
	{
		$sql = "SELECT id FROM merchant WHERE name = '$name'";
		$rs	 = mysql_query($sql, $this->intLinkSign) or die ("error: " . mysql_error());
		if(mysql_num_rows($rs) > 0)
			return true;
		else
			return false;
	}
	

}
?>
